package com.coastline.fruit.common.utils;

import com.coastline.fruit.common.constants.CommonConstant;

/**
 * 安全随机数工具类
 *
 * @author Coastline
 * @date 2025/1/8 22:11
 */
public class SecureRandomUtil {
    /**
     * 获取指定位数的密码
     *
     * @param passwordDigit 密码长度
     * @return password 密码
     */
    public static String getPassWord(int passwordDigit) {
        char[] chs = new char[passwordDigit];
        for (int i = 0; i < CommonConstant.pswdStr.length; i++) {

            int idx = (int) (Math.random() * CommonConstant.pswdStr[i].length());
            chs[i] = CommonConstant.pswdStr[i].charAt(idx);

        }

        for (int i = CommonConstant.pswdStr.length; i < passwordDigit; i++) {

            int arrIdx = (int) (Math.random() * CommonConstant.pswdStr.length);
            int strIdx = (int) (Math.random() * CommonConstant.pswdStr[arrIdx].length());

            chs[i] = CommonConstant.pswdStr[arrIdx].charAt(strIdx);
        }

        for (int i = 0; i < 1000; i++) {
            int idx1 = (int) (Math.random() * chs.length);
            int idx2 = (int) (Math.random() * chs.length);

            if (idx1 == idx2) {
                continue;
            }

            char tempChar = chs[idx1];
            chs[idx1] = chs[idx2];
            chs[idx2] = tempChar;
        }

        return new String(chs);
    }
}
